package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.entity.GoodsPictureEntity;
import com.yimils.core.goods.entity.PictureEntity;
import com.yimils.core.goods.mapper.GoodsPictureMapper;
import com.yimils.core.goods.serivce.GoodsPictureService;
import com.yimils.core.goods.serivce.PictureService;
import com.yimils.core.goods.vo.GoodsPictureRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("goodsPictureService")
public class GoodsPictureServiceImpl extends ServiceImpl<GoodsPictureMapper, GoodsPictureEntity> implements GoodsPictureService {
    @Resource(name = "pictureService")
    PictureService pictureService;

    @Override
    public Boolean saveGoodsPicture(List<String> pictureUrls, Long goodsId) {
        List<PictureEntity> entities = pictureService.savePictureList(pictureUrls);
        List<GoodsPictureEntity> goodsPictureEntities = entities.stream().map(o -> {
            GoodsPictureEntity goodsPictureEntity = new GoodsPictureEntity();
            goodsPictureEntity.setGoodsId(goodsId);
            goodsPictureEntity.setPictureId(o.getId());
            goodsPictureEntity.setState(Boolean.FALSE);
            goodsPictureEntity.setCreateTime(LocalDateTime.now());
            goodsPictureEntity.setUpdateTime(LocalDateTime.now());
            return goodsPictureEntity;
        }).collect(Collectors.toList());
        return saveBatch(goodsPictureEntities);
    }

    @Override
    public Boolean deleteGoodsPicture(List<Long> pictureIds, Long goodsId) {
        QueryWrapper<GoodsPictureEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsPictureEntity::getGoodsId, goodsId)
                .eq(GoodsPictureEntity::getState, Boolean.FALSE);
        List<GoodsPictureEntity> entities = list(qw);
        entities.forEach(o -> {
            o.setState(Boolean.TRUE);
            o.setUpdateTime(LocalDateTime.now());
        });
        saveOrUpdateBatch(entities);
        List<GoodsPictureEntity> collect = pictureIds.stream().map(o -> {
            GoodsPictureEntity goodsPictureEntity = new GoodsPictureEntity();
            goodsPictureEntity.setGoodsId(goodsId);
            goodsPictureEntity.setPictureId(o);
            goodsPictureEntity.setState(Boolean.FALSE);
            goodsPictureEntity.setCreateTime(LocalDateTime.now());
            goodsPictureEntity.setUpdateTime(LocalDateTime.now());
            return goodsPictureEntity;
        }).collect(Collectors.toList());
        return saveOrUpdateBatch(collect);
    }

    @Override
    public Map<Long, List<GoodsPictureRes>> mapListGoodsPicture(List<Long> goodsIds) {
        QueryWrapper<GoodsPictureEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(GoodsPictureEntity::getGoodsId, goodsIds)
                .eq(GoodsPictureEntity::getState, Boolean.FALSE);
        List<GoodsPictureEntity> entities = list(qw);
        List<Long> pictureIds = entities.stream().map(GoodsPictureEntity::getPictureId).collect(Collectors.toList());
        List<PictureEntity> pictureEntities = pictureService.listPicture(pictureIds);
        Map<Long, String> videoMap = pictureEntities.stream().collect(Collectors.toMap(PictureEntity::getId, PictureEntity::getUrl));
        return entities.stream().map(o -> {
            GoodsPictureRes res = new GoodsPictureRes();
            res.setGoodsId(o.getGoodsId());
            res.setPictureId(o.getPictureId());
            res.setUrl(videoMap.get(o.getId()));
            return res;
        }).collect(Collectors.groupingBy(GoodsPictureRes::getGoodsId));
    }
}
